package Humburg.dao;

import Snacks.dao.BaseDao;
import Snacks.dao.SnacksDao;
import Snacks.entiy.Snacks;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class HumburgDaoImpl extends BaseDao implements SnacksDao {
    @Override
    public int addInfor(Snacks snacks) {
        String sql="INSERT INTO `BurgerKingDB`.`snacks`(`snacksName`, `snacksPtion`, `snacksPrice`, `snacksFeature`, `snackTime`, `Shelf`, `snacksYhPrice`, `Description`, `BevegPrice`,`Classification`,Directory,recommend) VALUES (?,?,?,?,now(),?,?,?,?,3,?,1);";
        return this.executeUpdate(sql,snacks.getSnacksName(),snacks.getSnacksPtion(),snacks.getSnacksPrice(),snacks.getSnackFeature(),snacks.getShelf(),snacks.getSnacksYhPrice(),snacks.getTupian(),snacks.getHouzui(),snacks.getHaoneid());
    }

    @Override
    public int deletInfor(Snacks s) {
        String sql="delete from snacks where snacksld=?;";
        return this.executeUpdate(sql,s.getSnacksld());
    }

    @Override
    public List<Snacks> getLike(String name) {
        String sql="SELECT * FROM snacks WHERE snacksName LIKE '%" + name+"%' ;";
        ResultSet rs=this.executeQuery(sql);
        List<Snacks> list=new ArrayList<>();
        try {
            while (rs.next()) {
                list.add(new Snacks(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getDouble(4), rs.getString(5),rs.getString(6),rs.getInt(7),rs.getDouble(8),rs.getString(9),rs.getString(10),rs.getInt(13)));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            this.closeConnection(null,null,rs);
        }
        return list;
    }

    @Override
    public int getCount() {
        String sql="select count(*) from snacks where Classification=3";
        ResultSet rs=this.executeQuery(sql);
        try {
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        finally {
            closeConnection(null,null,rs);
        }
        return 0;
    }

    @Override
    public List<Snacks> getFyList(int page, int pagesize) {
        String sql="SELECT * FROM snacks where Classification=3 limit ?,?;";
        ResultSet rs=null;
        List<Snacks> list=new ArrayList<>();
        try {
            rs=this.executeQuery(sql,(page-1)*pagesize,pagesize);
            while (rs.next()) {
                Snacks snacks=new Snacks();
                snacks.setSnacksld(rs.getInt(1));
                snacks.setSnacksName(rs.getString(2));
                snacks.setSnacksPtion(rs.getString(3));
                snacks.setSnacksPrice(rs.getDouble(4));
                snacks.setSnackFeature(rs.getString(5));
                snacks.setSnackTime(rs.getString(6));
                snacks.setShelf(rs.getInt(7));
                snacks.setSnacksYhPrice(rs.getDouble(8));
                snacks.setTupian(rs.getString(9));
                snacks.setHouzui(rs.getString(10));
                snacks.setRecommend(rs.getInt(13));
                list.add(snacks);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            this.closeConnection(null,null,rs);
        }
        return list;
    }

    @Override
    public int upataInfor(Snacks s) {
        String sql="UPDATE snacks SET shelf = 0 WHERE snacksld = ?;";
        return this.executeUpdate(sql,s.getSnacksld());
    }

    @Override
    public int upataSInfor(Snacks s) {
        String sql="UPDATE snacks SET shelf = 1 WHERE snacksld = ?;";
        return this.executeUpdate(sql,s.getSnacksld());
    }
    @Override
    public int upataRemenn(Snacks s) {
        String sql="UPDATE snacks SET recommend = 1 WHERE snacksld = ?;";
        return this.executeUpdate(sql,s.getSnacksld());
    }

    @Override
    public int upataXRemenn(Snacks s) {
        String sql="UPDATE snacks SET recommend = 0 WHERE snacksld = ?;";
        return this.executeUpdate(sql,s.getSnacksld());
    }
}
